package com.d3works.apps.supplychain.web;

import com.d3works.apps.supplychain.db.Mrp;
import com.d3works.apps.supplychain.db.MrpLine;
import com.d3works.apps.supplychain.db.repo.MrpLineRepository;
import com.d3works.apps.supplychain.db.repo.MrpRepository;
import com.d3works.apps.supplychain.service.MrpLineService;
import com.d3works.apps.supplychain.service.MrpService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.service.TraceBackService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;
import com.google.inject.persist.Transactional;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;

@Singleton
public class MrpLineController {

  public void generateProposal(ActionRequest request, ActionResponse response)
      throws D3worksException {
    MrpLine mrpLine = request.getContext().asType(MrpLine.class);
    Beans.get(MrpLineService.class)
        .generateProposal(Beans.get(MrpLineRepository.class).find(mrpLine.getId()));
    response.setInfo(I18n.get("The proposal has been successfully generated."));
    response.setReload(true);
  }

  @Transactional(rollbackOn = {Exception.class})
  public void select(ActionRequest request, ActionResponse response) {
    toggle(request, response, true);
  }

  @Transactional(rollbackOn = {Exception.class})
  public void unselect(ActionRequest request, ActionResponse response) {
    toggle(request, response, false);
  }

  @SuppressWarnings("unchecked")
  @Transactional(rollbackOn = {Exception.class})
  private void toggle(ActionRequest request, ActionResponse response, boolean proposalToProcess) {
    try {
      List<Integer> mrpLineIds = (List<Integer>) request.getContext().get("_ids");

      if (CollectionUtils.isNotEmpty(mrpLineIds)) {
        Beans.get(MrpLineService.class).updateProposalToProcess(mrpLineIds, proposalToProcess);
      }

      response.setAttr("mrpLinePanel", "refresh", true);
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  @Transactional(rollbackOn = {Exception.class})
  public void toggleOne(ActionRequest request, ActionResponse response) {
    try {
      MrpLine mrpLine = request.getContext().asType(MrpLine.class);
      mrpLine = Beans.get(MrpLineRepository.class).find(mrpLine.getId());

      Beans.get(MrpLineService.class)
          .updateProposalToProcess(mrpLine, !mrpLine.getProposalToProcess());

      response.setAttr("mrpLinePanel", "refresh", true);
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  @Transactional(rollbackOn = {Exception.class})
  public void selectAll(ActionRequest request, ActionResponse response) {
    try {
      Mrp mrp = request.getContext().getParent().asType(Mrp.class);
      mrp = Beans.get(MrpRepository.class).find(mrp.getId());

      Beans.get(MrpService.class).massUpdateProposalToProcess(mrp, true);

      response.setAttr("mrpLinePanel", "refresh", true);
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
